package com.lottery.im.module.service.ui;

import android.annotation.SuppressLint;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.msg_bulletin.BulletionDetails;
import com.lottery.im.api.result.entity.msg_bulletin.MessageDetails;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.presenter.MessageAndBulletinDetailPresenter;
import com.lottery.im.module.service.view.MessageAndBulletinDetailView;
import com.lottery.im.view.HtmlWebView;
import me.wcy.htmltext.HtmlText;

@SuppressLint("ValidFragment") public class MessageAndBulletinDetailFragment
    extends BaseSwipeFragment<MessageAndBulletinDetailPresenter>
    implements MessageAndBulletinDetailView {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.title_webView) HtmlWebView titleWebView;
  @BindView(R.id.msgTitle) TextView titleTextView;
  @BindView(R.id.mstTime) TextView mstTime;
  @BindView(R.id.webView) WebView webView;
  private String title;
  private long msgId;
  private int bulletinId;

  @SuppressLint("ValidFragment")
  public MessageAndBulletinDetailFragment(String title, int bulletinId) {
    this.title = title;
    this.bulletinId = bulletinId;
  }

  public MessageAndBulletinDetailFragment(String title, long msgId) {
    this.title = title;
    this.msgId = msgId;
  }

  @Override protected int getLayoutResId() {
    return R.layout.message_bulletin_detail_item;
  }

  @Override protected void initPresenter() {
    presenter = new MessageAndBulletinDetailPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    tvTitle.setText(title);
    if (getString(R.string.message_detail).equals(title)) {
      presenter.loadMessageDetailSuccess(msgId);
    } else if (getString(R.string.bulletin_detail).equals(title)) {
      presenter.loadBulletinDetailSuccess(bulletinId);
    }
  }

  @Override public void loadMesssgeDetailSuccess(MessageDetails messageDetails) {
    titleWebView.setVisibility(View.VISIBLE);titleTextView.setVisibility(View.GONE);
    mstTime.setPadding(10,0,0,0);
    titleWebView.loadDataWithBaseURL(null, messageDetails.getMessageTitle(),
        "text/html", "UTF-8", null);
    mstTime.setText(messageDetails.getCreateTime());
    webView.loadDataWithBaseURL(null, messageDetails.getMessage(),
        "text/html", "UTF-8", null);
  }

  @Override public void loadBullitenDetailSuccess(BulletionDetails bulletionDetails) {
    titleWebView.setVisibility(View.GONE);titleTextView.setVisibility(View.VISIBLE);
    mstTime.setPadding(0,10,10,10);
    HtmlText.from(bulletionDetails.getTitle()).into(titleTextView);
    mstTime.setText(bulletionDetails.getCreateDate());
    webView.loadDataWithBaseURL(null, bulletionDetails.getContent(),
        "text/html", "UTF-8", null);
  }

  @OnClick(R.id.iv_back) public void onViewClicked() {
    popBackStack();
  }

}
